import threading
import time

from selenium.webdriver import Chrome
from django.conf import settings
from selenium import webdriver

from forleave.models import ForLeaveRecord


class AskForLeave(threading.Thread):
    def __init__(self, data):
        super().__init__()

        # 隐藏浏览器
        chrome_opts = webdriver.ChromeOptions()
        chrome_opts.add_argument("--headless")

        # 加载驱动
        self.driver = Chrome(executable_path=settings.DRIVER_PATH, chrome_options=chrome_opts)
        self.data = data
        self.record = ForLeaveRecord()
        self.record.student_id = self.data.id

    def handle(self):
        IMAGE_TIP_PATH = self.data.image_tip_path()
        IMAGE_RECORD_PATH = self.data.image_record_path()

        # 登录
        self.driver.get(settings.WEB_LOGIN_URL)
        time.sleep(0.5)
        self.driver.find_element_by_id('username').send_keys(self.data.username)
        self.driver.find_element_by_id('password').send_keys(self.data.password)
        self.driver.find_element_by_class_name('auth_login_btn').click()

        # 跳转到请假页面
        self.driver.get(settings.WEB_LEAVE_URL)
        time.sleep(0.5)

        # 选择校区
        self.driver.find_element_by_css_selector('#select2-select_xq-container').click()
        time.sleep(0.5)
        if self.data.area == 0:
            self.driver.find_element_by_css_selector('li[id*="临港校区"]').click()
        elif self.data.area == 1:
            self.driver.find_element_by_css_selector('li[id*="闵行校区"]').click()

        # 联系电话
        self.driver.find_element_by_css_selector('#input_lxdh').send_keys(self.data.mobile)

        # 紧急联系电话
        self.driver.find_element_by_css_selector('#input_jjlxdh').send_keys(self.data.mobile_spe)

        # 离校返校时间自动输入
        self.driver.find_element_by_css_selector('.date-start > input:nth-child(1)').click()
        self.driver.find_element_by_css_selector('.date-start > input:nth-child(1)').click()
        self.driver.find_element_by_css_selector('.date-end > input:nth-child(1)').click()
        self.driver.find_element_by_css_selector('#hourselect_left_lxsjdaterange_end').click()
        self.driver.find_element_by_css_selector('#hourselect_left_lxsjdaterange_end > option:nth-child(24)').click()
        self.driver.find_element_by_css_selector(
            'div.daterangepicker:nth-child(5) > div:nth-child(3) > div:nth-child(1) > button:nth-child(1)').click()

        # 目的地
        self.driver.find_element_by_css_selector('#input_mdd').send_keys(self.data.target)

        # 申请原因
        if self.data.reason == 0:
            self.driver.find_element_by_css_selector('#radio_sqly1').click()
        elif self.data.reason == 1:
            self.driver.find_element_by_css_selector('#radio_sqly3').click()
        elif self.data.reason == 2:
            self.driver.find_element_by_css_selector('#radio_sqly5').click()

        # 下一步
        self.driver.find_element_by_css_selector('.item-value.participants-next').click()
        # if self.data.next == 0:
        #     self.driver.find_element_by_css_selector('#select2-kec5-result-1mh3-end').click()
        # elif self.data.next == 1:
        #     self.driver.find_element_by_css_selector('#select2-kec5-result-gawd-fdysh').click()

        # 提交按钮点击
        self.driver.find_element_by_css_selector('.sui-submit > a:nth-child(1)').click()
        time.sleep(1.5)
        self.driver.get_screenshot_as_file(IMAGE_TIP_PATH)

        # 进入一站式办事大厅截图
        self.driver.get(settings.WEB_HOME_URL)
        time.sleep(0.5)
        self.driver.find_element_by_css_selector('.smjtable2 > a:nth-child(1)').click()
        self.driver.find_element_by_css_selector(
            '#table2 > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2) > a:nth-child(1)').click()
        time.sleep(1.5)
        self.driver.get_screenshot_as_file(IMAGE_RECORD_PATH)

        # disposed
        self.record.image_tip = IMAGE_TIP_PATH
        self.record.image_record = IMAGE_RECORD_PATH
        self.record.save()
        self.driver.close()

    def run(self):
        try:
            self.handle()
        except Exception as e:
            print(self.data)
            print(e)
            self.driver.close()
